﻿using Dapper;
using System;
using BCEntity.Common.Solution.WebsiteSolution;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.Common.Solution.WebsiteSolution
{
    /// <summary>
    /// 官网解决方案
    /// </summary>
    public class WebsiteSolutionData : IWebsiteSolutionData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY_EXISTS = @"SELECT 1 FROM websitesolution";
        private const string QUERY = @"SELECT WebsiteSolutionId,CreateAdminName,CreateTime,UpdateTime,EditAdminId,EditAdminName,SolutionName,Description,Summary,Icon,Background,LinkUrl,Enabled,CreateAdminId FROM websitesolution";
        public WebsiteSolutionData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }
        #region 检查官网解决方案是否存在
        public async Task<bool> Exists(int websiteSolutionId)
        {
            string sql = string.Format(@"{0} WHERE WebsiteSolutionId=?WebsiteSolutionId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteSolutionId", websiteSolutionId, DbType.Int32);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }
        #endregion

        #region 添加官网解决方案
        public async Task<WebsiteSolutionEntity> Add(WebsiteSolutionEntity entity)
        {
            string sql = SqlCommands.GetInsertSql("websitesolution", new string[] { "SolutionName", "Description", "Summary", "Icon", "Background", "LinkUrl", "CreateAdminName", "CreateAdminId" });
            sql = string.Format("{0} {1} WHERE WebsiteSolutionId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SolutionName", entity.SolutionName?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?Description", entity.Description?.Trim(), DbType.String);
            parameters.Add("?Summary", entity.Summary?.Trim(), DbType.String);
            parameters.Add("?Icon", entity.Icon?.Trim(), DbType.AnsiString, size: 1000);
            parameters.Add("?Background", entity.Background?.Trim(), DbType.AnsiString, size: 1000);
            parameters.Add("?LinkUrl", entity.LinkUrl?.Trim(), DbType.AnsiString, size: 1000);
            parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
            parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WebsiteSolutionEntity>(sql, parameters, databaseContext.Transaction);

        }
        #endregion

        #region 修改官网解决方案
        public async Task<WebsiteSolutionEntity> Update(WebsiteSolutionEntity entity)
        {
            string sql = SqlCommands.GetUpdateSql("websitesolution", new string[] { "EditAdminId", "EditAdminName", "SolutionName", "Description", "Summary", "Icon", "Background", "LinkUrl" }, new string[] { "WebsiteSolutionId" });
            sql = string.Format("{0} {1} WHERE WebsiteSolutionId=?WebsiteSolutionId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteSolutionId", entity.WebsiteSolutionId, DbType.Int32, size: 11);
            parameters.Add("?EditAdminId", entity.EditAdminId, DbType.Int64, size: 20);
            parameters.Add("?EditAdminName", entity.EditAdminName?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?SolutionName", entity.SolutionName?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?Description", entity.Description?.Trim(), DbType.String);
            parameters.Add("?Summary", entity.Summary?.Trim(), DbType.String);
            parameters.Add("?Icon", entity.Icon?.Trim(), DbType.AnsiString, size: 1000);
            parameters.Add("?Background", entity.Background?.Trim(), DbType.AnsiString, size: 1000);
            parameters.Add("?LinkUrl", entity.LinkUrl?.Trim(), DbType.AnsiString, size: 1000);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WebsiteSolutionEntity>(sql, parameters, databaseContext.Transaction);
        }
        #endregion

        #region 删除官网解决方案
        public async Task<bool> Delete(int websiteSolutionId)
        {
            string sql = @"DELETE FROM websitesolution WHERE WebsiteSolutionId=?WebsiteSolutionId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteSolutionId", websiteSolutionId, DbType.Int32);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion

        #region 获取官网解决方案
        public async Task<WebsiteSolutionEntity> GetEntity(int websiteSolutionId)
        {
            string sql = string.Format(@"{0} WHERE WebsiteSolutionId=?WebsiteSolutionId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteSolutionId", websiteSolutionId, DbType.Int64);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<WebsiteSolutionEntity>(sql, parameters);
        }
        #endregion

        #region 获取官网解决方案列表
        public async Task<IEnumerable<WebsiteSolutionEntity>> GetList()
        {
            string sql = string.Format(@"{0} WHERE Enabled=1", QUERY);
            return await this.databaseContext.Slave.QueryAsync<WebsiteSolutionEntity>(sql);
        }
        #endregion

        #region 获取官网解决方案分页
        public async Task<(IEnumerable<WebsiteSolutionEntity>, int)> Query(bool? enabled, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (enabled.HasValue)
            {
                wheres.Add("Enabled=?Enabled");
                parameters.Add("?Enabled", enabled.Value, DbType.Boolean);
            }
            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM websitesolution {1}",
                  QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
                  pageSize,
                  string.IsNullOrWhiteSpace(sortName) ? "CreateTime" : sortName,
                  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
            return await this.databaseContext.Slave.QueryMultipleAsync<WebsiteSolutionEntity, int>(sql, parameters);
        }
        #endregion

        #region 启用官网解决方案
        public async Task<bool> Enabled(int websiteSolutionId)
        {
            string sql = @"UPDATE websitesolution SET Enabled=1 WHERE WebsiteSolutionId=?WebsiteSolutionId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteSolutionId", websiteSolutionId, DbType.Int32);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion

        #region 禁用官网解决方案
        public async Task<bool> Disabled(int websiteSolutionId)
        {
            string sql = @"UPDATE websitesolution SET Enabled=0 WHERE WebsiteSolutionId=?WebsiteSolutionId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteSolutionId", websiteSolutionId, DbType.Int32);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion
    }
}



